﻿@{
    ViewBag.Title = "【华志创--排课任务详细】";
    Layout = "~/Views/Shared/___newestMaster.cshtml";
}

@using ClassModel
@using ClassBLL
@using ClassCommon
@using System.Data
@using System.Data.SqlClient

@{
    t_classDemo_UserInfo currentLoginer = ViewBag.CurrentUser;
    t_class_task task = ViewBag.Task;
    // 根据任务获取是否已经上传了Excel表格
    t_class_fileupload excelFile = t_class_user_fileupalod_BLL.GetExcelFileWithTaskId(task.Id, currentLoginer.id);
    t_class_sections section = t_class_sections_BLL.GetSectionByTaskID(task.Id);

    string ruleHref = "ruleIndex?taskid=" + ViewBag.TaskId;

    bool hasSection = t_class_sections_BLL.isSectionIsExistsWithTaskId(task.Id);
    if (!hasSection)
    {
        ruleHref = "javascript:void(0);";
    }
}

@section style {
    <link href="../../Static/Template/css/webuploader.css" rel="stylesheet" />

    <style type="text/css">
        .webuploader-element-invisible {
            position: absolute !important;
            clip: rect(1px 1px 1px 1px);
        }

        #picker div:nth-child(2) {
            opacity: 0.2;
        }


        .bigimg input:nth-child(4) {
            display: inline-block;
            width: 15%;
            position: absolute;
            right: 40px;
            background: 0;
            cursor: pointer;
            border: 0;
            height: 41px;
            outline: none;
            color: #FFFFFF;
            border: 1px solid #4CB644;
            background: #4CB644;
            border-radius: 20px;
        }

            .bigimg input:nth-child(4):hover {
                border: 1px solid #6ad062;
                background: #6ad062;
            }
    </style>
}

<div class="jcxx">
    <div class="conConT">
        <ul>
            <li>我的任务：</li>
            <li>@(task.Name)</li>
            <li>
                <input type="button" name="" id="downloadTemplate" value="下载课表Excel表格模板" />
            </li>
            <li>
                <input type="button" name="" id="" value="点击上传" />
            </li>
        </ul>
    </div>
    <div class="conConTo col-lg-12 col-md-12 col-sm-12">
        <ul>
            <li>任课信息</li>
            <li>
                <p>请按照模板样式填写课时分配和任课安排，注意阅读红色标注。</p>
                @if (section != null)
                {
                    <p>
                        您已经设置节次信息。【
                    <span>上午@(section.AMSections)节</span>】【
                    <span>下午@(section.PMSections)节</span>】【
                    <span>一周@(section.dayOfWeek)天</span>】
                    </p>     
                }
            </li>
        </ul>
    </div>
    <div class="table-responsive" style="padding-top: 30px;">

        @if (excelFile != null)
        {
            string filePath = HttpContext.Current.Server.MapPath(excelFile.savePath);
            DataTable dt = null;

            if (excelFile.extensionName == ".xls")
            {
                // 文件路径
                ExcelHelper eh = new ExcelHelper(filePath);
                dt = eh.ExcelToDataTable("Sheet1", true);
            }
            else if (excelFile.extensionName == ".csv")
            {
                dt = CsvParsingHelper.CsvToDataTable(filePath, true);
            }
            
            <table class="table">
                <thead>
                    @Html.Raw(HTMLBuilderHelper.BuilderColsWithDataTable(dt))
                </thead>
                <tbody>
                    @Html.Raw(HTMLBuilderHelper.BuilderRowsWithDataTable(dt))
                </tbody>
            </table>
        }
        else
        { // 用户没有上传课表excel文件
            <table class="table">
                <thead>
                    <tr>
                        <th>年级</th>
                        <th>班级</th>
                        <th>语文</th>
                        <th></th>
                        <th>数学</th>
                        <th></th>
                        <th>外语</th>
                        <th></th>
                        <th>艺术</th>
                        <th></th>
                        <th>劳技</th>
                        <th></th>
                        <th>体育</th>
                        <th></th>
                        <th>班会</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>高二</td>
                        <td>1班</td>
                        <td>3+1</td>
                        <td>宇文瑞</td>
                        <td>5</td>
                        <td>于志成</td>
                        <td>5</td>
                        <td>童向娇</td>
                        <td>1</td>
                        <td>司马凝竹</td>
                        <td>1</td>
                        <td>苟明远</td>
                        <td>1</td>
                        <td>宜采萱</td>
                        <td>阿达瓦</td>
                        <td>1</td>
                    </tr>
                    <tr>
                        <td>高二</td>
                        <td>1班</td>
                        <td>3+1</td>
                        <td>宇文瑞</td>
                        <td>5</td>
                        <td>于志成</td>
                        <td>5</td>
                        <td>童向娇</td>
                        <td>1</td>
                        <td>司马凝竹</td>
                        <td>1</td>
                        <td>苟明远</td>
                        <td>1</td>
                        <td>宜采萱</td>
                        <td>阿达瓦</td>
                        <td>1</td>
                    </tr>
                    <tr>
                        <td>高二</td>
                        <td>1班</td>
                        <td>3+1</td>
                        <td>宇文瑞</td>
                        <td>5</td>
                        <td>于志成</td>
                        <td>5</td>
                        <td>童向娇</td>
                        <td>1</td>
                        <td>司马凝竹</td>
                        <td>1</td>
                        <td>苟明远</td>
                        <td>1</td>
                        <td>宜采萱</td>
                        <td>阿达瓦</td>
                        <td>1</td>
                    </tr>
                    <tr>
                        <td>高二</td>
                        <td>1班</td>
                        <td>3+1</td>
                        <td>宇文瑞</td>
                        <td>5</td>
                        <td>于志成</td>
                        <td>5</td>
                        <td>童向娇</td>
                        <td>1</td>
                        <td>司马凝竹</td>
                        <td>1</td>
                        <td>苟明远</td>
                        <td>1</td>
                        <td>宜采萱</td>
                        <td>阿达瓦</td>
                        <td>1</td>
                    </tr>
                </tbody>
            </table>
        }
    </div>
</div>


<div class="vbig col-lg-4 col-md-4 col-sm-3">
    <input type="button" name="" id="nextStep" value="下一步" />
    <input type="button" name="" id="prevStep" onclick="window.history.go(-1)" value="上一步" />
</div>

<!-- 是否设置了节次信息 -->
<input type="hidden" id="hasSections" value="@(section == null ? "0" : "1")"/>
<!-- 节次设置弹窗。 -->
<div class="bg1" style="display: none;">
    <div class="container">
        <div class="row">
            <div class="col-lg-4 col-md-7 col-sm-10 tc1">
                <div class="ttc1">
                    <span>设置上课时间</span>
                    <span>修改后，设置的规则和预排会跟着进行相对变化。</span>
                    <span></span>
                </div>
                <p>请对以下选项进行选择设置</p>
                <p>
                    每周：<i></i>
                    <select name="daysPerWeek" id="daysPerWeek">
                        <option value="1">1天</option>
                        <option value="2">2天</option>
                        <option value="3">3天</option>
                        <option value="4">4天</option>
                        <option value="5">5天</option>
                    </select>
                </p>
                <p>
                    上午：<i></i>
                    <select name="amLessonCount" id="amLessonCount">
                        <option value="1">1节</option>
                        <option value="2">2节</option>
                        <option value="3">3节</option>
                        <option value="4">4节</option>
                        <option value="5">5节</option>
                        <option value="6">6节</option>
                    </select>
                </p>
                <p>
                    下午：<i></i>
                    <select name="pmLessonCount" id="pmLessonCount">
                        <option value="1">1节</option>
                        <option value="2">2节</option>
                        <option value="3">3节</option>
                        <option value="4">4节</option>
                    </select>
                </p>
                <p class="col-lg-6 col-md-8 col-sm-10 TJ">
                    <input type="button" name="" id="bc1" value="保存" />
                    @* <input type="button" name="" id="qx1" value="取消" />*@
                </p>
            </div>
        </div>
    </div>
</div>

<!-- 如果有已经上传的file文件，这里就出现当前上传文件的fileid -->
<input type="hidden" id="fileId" value="@(excelFile != null ? excelFile.id.ToString() : "0")" />
<input type="hidden" id="taskId" />
<input type="hidden" id="extension" value="@(excelFile != null ? excelFile.extensionName : string.Empty)" />

@section script {
    <script src="../../Static/Template/js/webuploader.js"></script>

    <script type="text/javascript">
        $(function () {

            // 下载模板
            $("#downloadTemplate").on("click", function () {
                window.open("/Static/示例数据-任课-课时.csv");
            })

            // 任务id
            var taskId = getQueryVariable("id");

            // 获取taskID
            (function () {
                $("#taskId").val(taskId);
            })();


            // 创建上传对象
            var uploader = WebUploader.create({
                auto: true,
                swf: '/Static/webuploader/Uploader.swf',
                // 后台服务器文件上传地址
                server: "/Utility/fileupload",
                formData: { taskId: $("#taskId").val(), typeValue: "csv", fileId: $("#fileId").val() },
                pick: '#picker',
                resize: false,
                accept: {
                    title: 'Excel',
                    extensions: 'xls,csv',
                    mimeTypes: 'application/excel'
                }
            });

            // 文件上传成功
            uploader.on('uploadSuccess', function (file, response) {
                $('#' + file.id).find('p.state').text('已上传');
                // console.log(response);
                if (response.status) {
                    // 重新刷新页面，并完成从 excel 到 table的转变。
                    window.location.reload();
                }
            });

            uploader.on('uploadProgress', function (file, percentage) {
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress .progress-bar');
                // 避免重复创建
                if (!$percent.length) {
                    $percent = $('<div class="progress progress-striped active">' +
                    '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                    '</div>' +
                    '</div>').appendTo($li).find('.progress-bar');
                }
                $li.find('p.state').text('上传中');
                $percent.css('width', percentage * 100 + '%');
            });


            // 是否设置了节次信息
            $("#nextStep").on("click", function () {
                // 是否上传了excel文件
                var hasExcelFile = $("#fileId").val();
                if (hasExcelFile == "0") {
                    $(".bg3").css("display", "block");

                    return;
                }


                var hasSections = $("#hasSections").val();
                if (hasSections == "0") {
                    $(".bg1").css("display", "block");
                    return;
                }

                var extension = $("#extension").val();
                if (extension == ".csv") {
                    showFailure("csv文件功能快速开发中!");
                    return;
                }

                // 进入到规则条件页面
                window.location.href = "ruleIndex?taskid=" + taskId;


            })

            $("#bc1").on("click", function () {
                saveClassJieCiInfo();
            });


            // 节次信息提交
            function saveClassJieCiInfo() {
                var daysPerWeek = $("#daysPerWeek").find("option:selected").val();
                var amLessonCount = $("#amLessonCount").find("option:selected").val();
                var pmLessonCount = $("#pmLessonCount").find("option:selected").val();
                // 提交节次信息
                $.post("SettingSection", {
                    taskid: taskId,
                    dayofweek: daysPerWeek,
                    amSections: amLessonCount,
                    pmSections: pmLessonCount
                }, function (data) {
                    if (data.status) {
                        window.location.reload();
                    } else {
                        if (data.info2) {
                            alert(data.info2);
                        }
                    }
                });
            }
        })

        // 获取查询字符参数
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (false);
        }

    </script>
}